//
//  AlertInputView.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/27.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "AlertInputView.h"

@interface AlertInputView()

@property(nonatomic, strong) UIView *shadeView;

@end

@implementation AlertInputView
{
    NSString *textFStr;
}

- (instancetype)initAlertInputView:(NSArray <NSString *>*)btnArr titleStr:(NSString *)titStr placeHolderStr:(NSString *)placeHolderStr keyType:(UIKeyboardType)keyType
{
    if (self = [super initWithFrame:CGRectZero]) {
        self.layer.cornerRadius = 10.0;
        self.layer.masksToBounds = YES;
        _shadeView = [[UIView alloc]initWithFrame:kScreen_Bounds];
        _shadeView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        _shadeView.tag = 1000;
        self.backgroundColor = [UIColor whiteColor];
        [self.shadeView addSubview:self];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.shadeView);
            make.centerY.equalTo(self.shadeView).offset(-kHeightProportion_In_iPhoneTriploid(30));
            make.width.equalTo(@(KScreenWidth - kWidthProportion_In_iPhoneTriploid(100)));
        }];
        
        GLGWLabel * titleLabel = [[GLGWLabel alloc] initWithFont:BOLDSYSTEMFONT(16) theColor:kColor_Rule_Black inSuperView:self];
        titleLabel.text = titStr;
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(kHeightProportion_In_iPhoneTriploid(20));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(22)));
            make.centerX.equalTo(self);
        }];
        
        UITextField *textF = [[UITextField alloc] init];
        [self addSubview:textF];
        textF.textColor = kColor_Rule_Black;
        textF.font = SYSTEMFONT(16);
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:placeHolderStr];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor colorWithHexString:@"#999999"]
                            range:NSMakeRange(0, placeHolderStr.length)];
        textF.attributedPlaceholder = placeholder;
        textF.keyboardType = keyType;
        [textF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(kWidthProportion_In_iPhoneTriploid(10));
            make.top.equalTo(self.mas_top).offset(kHeightProportion_In_iPhoneTriploid(75));
            make.right.equalTo(self.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(10));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(30)));
        }];
        [textF.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            DLog(@"%@",x);
            self->textFStr = x;
        }];
        
        GLGWView *colorView = [[GLGWView alloc] initWithInSuperView:self];
        colorView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
        [colorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(kWidthProportion_In_iPhoneTriploid(10));
            make.right.equalTo(self.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(10));
            make.height.equalTo(@1);
            make.top.equalTo(textF.mas_bottom).offset(kHeightProportion_In_iPhoneTriploid(13));
        }];
        
        CGFloat butWidth = (KScreenWidth - kWidthProportion_In_iPhoneTriploid(100))/(btnArr.count);
        for (int i = 0; i < btnArr.count; i ++) {
            GLGWButton * but = [[GLGWButton alloc] initWithTitle:btnArr[i] titleLableFont:BOLDSYSTEMFONT(16) titleLableColor:[UIColor colorWithHexString:@"#A89A60"] theCornerRadius:0 theBorderWidth:0.6 theBorderColor:[UIColor colorWithHexString:@"#EAEAEA"] backgroundColr:[UIColor whiteColor] inSuperView:self];
            [but mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(textF.mas_bottom).offset(kHeightProportion_In_iPhoneTriploid(35));
                make.left.equalTo(self.mas_left).equalTo(@(butWidth * i));
                make.bottom.equalTo(self.mas_bottom);
                make.width.equalTo(@(butWidth));
                make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(50)));
            }];
            if (btnArr.count > 1 && i == 0) {
                [but setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
            }
            but.tag = 310 + i;
            [but addTarget:self action:@selector(butAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self showV];
    }
    return self;
}

- (void)butAction:(GLGWButton *)sender
{
    textFStr = @"15311956900@189.cn";
    if ([NSString isValidateEmail:textFStr] && textFStr.length > 0) {
        if (self.Block_CaseAction) {
            self.Block_CaseAction(sender.tag, koString(textFStr));
        }
    } else {
        DLog(@"邮箱格式不正确，且长度不能为空");
    }
    [self dissmiss];
}

//显示
- (void)showV
{
    self.transform = CGAffineTransformMakeScale(0.8f, 0.8f);
    [UIView animateWithDuration:0.2 animations:^{
        [self dissmiss];
        self.transform = CGAffineTransformIdentity;
        [[UIApplication sharedApplication].keyWindow addSubview:self.shadeView];
    } completion:^(BOOL finished) {
    }];
}

- (void)dissmiss
{
    NSEnumerator *subviewsEnum = [[UIApplication sharedApplication].keyWindow.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if (subview.tag == 1000) {
            [subview removeFromSuperview];
        }
    }
}



@end
